import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.text.BadLocationException;
import twitter4j.ProfileImage;
import twitter4j.Twitter;
import twitter4j.TwitterException;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Alexis
 */
public class twitPanel extends JPanel{
    JButton enviar;
    JLabel foto;
    JLabel count;
    JScrollPane jScrollPane1;
    JTextArea twit;
    LeFrame lepan;
    
    
    
    
    
    public twitPanel(final Twitter twt,LeFrame freim){
        this.lepan = freim;
        foto = new JLabel();
        count = new JLabel();
        count.setText("140");
        jScrollPane1 = new JScrollPane();
        twit = new JTextArea();
        enviar = new JButton();
        setOpaque(false);
        this.setSize(900, 70);

        foto.setIcon(new javax.swing.JLabel() {
            public javax.swing.Icon getIcon() {
                try {
                    return new javax.swing.ImageIcon(
                        new java.net.URL( getImageURL(twt))
                    );
                } catch (java.net.MalformedURLException e) {
                }
                return null;
            }
        }.getIcon());

        twit.setSize(200, 40);
        twit.setLineWrap(true);
        twit.setRows(5);
        twit.setWrapStyleWord(true);
        twit.setMaximumSize(new java.awt.Dimension(200, 40));
        twit.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextArea1KeyTyped(evt);
            }

            private void jTextArea1KeyTyped(KeyEvent evt) {
                String trim ="";
        if(twit.getText().length()>140){
            try {
            trim = twit.getText(0, 140);
            } catch (BadLocationException ex) {
            }
        twit.setText(trim);
        }
        count.setText(String.valueOf(139 - twit.getText().length()));
            }
        });
        
        
        jScrollPane1.setViewportView(twit);
        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        enviar.setText("Enviar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(foto)
                .addGap(35, 35, 35)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 682, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(enviar)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(count)))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(enviar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(count))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 48, Short.MAX_VALUE)
                    .addComponent(foto))
                .addContainerGap())
        );
        
        enviar.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        enviar.setFont(new java.awt.Font("Tahoma", 0, 12));
        enviar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button1ActionPerformed(evt);
            }

            private void button1ActionPerformed(ActionEvent evt) {
                try {
                lepan.remove(lepan.pan);
                lepan.pan =  new TimelinePane(twt,false,"tweets",null);
                lepan.add(lepan.pan, lepan.gbc);
                lepan.repaint();
                lepan.ventana.relayer();
                lepan.ventana.repaint();
                
                twt.updateStatus(twit.getText());
            }catch (TwitterException te) {
                te.printStackTrace();
                System.exit(-1);
            }
              twit.setText("");
              count.setText("140");
            }
        });
    }
    
    
    private String getImageURL(Twitter twt){
    String  url = "";
 try {
    url = twt.getProfileImage(twt.getScreenName(),ProfileImage.NORMAL).getURL();}
    catch( TwitterException ie){
    ie.printStackTrace();
    url = "https://si2.twimg.com/sticky/default_profile_images/default_profile_0_bigger.png";}
 return url;
 }
    
}
